package com.ikaros.concurrency.example.singleton;

import com.ikaros.concurrency.annotations.Recommend;
import com.ikaros.concurrency.annotations.ThreadSafe;

/**
 * 枚举模式：最安全
 */
@ThreadSafe
@Recommend
public class SingletonEnumRecommend {
    private SingletonEnumRecommend() {
    }

    public static SingletonEnumRecommend getInstance() {
        return Singleton.INSTANCE.singletonEnumRecommend;
    }

    private enum Singleton {
        INSTANCE;
        private SingletonEnumRecommend singletonEnumRecommend;

        //jvm保证这个方法 绝对只调用一次
        Singleton() {
            singletonEnumRecommend = new SingletonEnumRecommend();
        }

        public SingletonEnumRecommend getInstance() {
            return singletonEnumRecommend;
        }
    }

    public static void main(String[] args) {
        System.out.println(SingletonEnumRecommend.getInstance().hashCode());
        System.out.println(SingletonEnumRecommend.getInstance().hashCode());
    }
}
